package com.cskaoyan.service.admin.promotion;

import com.cskaoyan.bean.MarketAdExample;
import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketCouponExample;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/*
 * @Auther:王贲
 * @Date:2022/9/7
 * @Description:
 * @VERSON:1.8
 */
@Service
@Transactional
public class AdminCouponServiceImpl implements AdminCouponService{

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;
    MarketCouponExample marketCouponExample;
    MarketCouponExample.Criteria criteria;

    /**
     * @description : TODO 显示优惠券页面和按要求查询优惠券的实现
     * @return com.cskaoyan.bean.common.CommonData
     * @author wang'ben
     * @date 2022/9/7 20:15
     */
    @Override
    public CommonData selectCouponInfo(BasePageInfo basePageInfo, String name, Integer type, Integer status) {
        //1.开启分页功能
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        marketCouponExample = new MarketCouponExample();
        criteria = marketCouponExample.createCriteria();
        marketCouponExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        //2. 模糊查询
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(type)) {
            if (type == 0) {
                criteria.andTypeEqualTo((short) 0);
            } else if (type == 1) {
                criteria.andTypeEqualTo((short) 1);
            } else if (type == 2) {
                criteria.andTypeEqualTo((short) 2);
            }
        }
        if (!StringUtils.isEmpty(status)) {
            if (status == 0) {
                criteria.andStatusEqualTo((short) 0);
            } else if (status == 1) {
                criteria.andStatusEqualTo((short) 1);
            } else if (status == 2) {
                criteria.andStatusEqualTo((short) 2);
            }
        }
        // 3. 查出所有数据
        List<MarketCoupon> list = marketCouponMapper.selectByExample(marketCouponExample);
        List<MarketCoupon> coupons = new ArrayList<>();
        //判断数据中是否逻辑删除
        for (MarketCoupon marketCoupon : list) {
            if (marketCoupon.getDeleted()) {
                continue;
            }
            coupons.add(marketCoupon);
        }

        long total = marketCouponMapper.countByExample(null);
        CommonData commonData = new CommonData<>();
        commonData.setTotal((int)total);
        commonData.setPages(basePageInfo.getPage());
        commonData.setLimit(basePageInfo.getLimit());
        commonData.setPage(basePageInfo.getPage());
        commonData.setList(coupons);
        return commonData;
    }

    /**
     * @description : TODO 添加优惠券方法的实现
     * @return void
     * @author wang'ben
     * @date 2022/9/8 18:10
     */
    @Override
    public void insertCouponInfo(MarketCoupon marketCoupon) {
        Short type = marketCoupon.getType();
        if (type == 2) {
            // 随机生成一个6位字符串插入code中
            String str = "";
            for (int i = 0; i < 6; i++) {
                int random = new Random().nextInt(10);
                str += random + "";
            }
            marketCoupon.setCode(str);
            marketCoupon.setDeleted(false);
            marketCouponMapper.insert(marketCoupon);

        } else {
            marketCoupon.setDeleted(false);
            marketCouponMapper.insert(marketCoupon);
        }

    }


    /**
     * @description : TODO 进入优惠券详情页面方法是现实
     * @return com.cskaoyan.bean.common.CommonData
     * @author wang'ben
     * @date 2022/9/8 18:11
     */
    @Override
    public CommonData selectListuserInfo(BasePageInfo basePageInfo, Integer couponId) {
        //1. 开启分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        marketCouponExample = new MarketCouponExample();
        criteria = marketCouponExample.createCriteria();
        marketCouponExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getSort());

        //2. 模糊查询
            MarketCouponUser marketCouponUser = marketCouponUserMapper.selectByPrimaryKey(couponId);
            List<MarketCouponUser> list = new ArrayList<>();
            list.add(marketCouponUser);
            long total = marketCouponUserMapper.countByExample(null);
            CommonData CommonData = new CommonData<>();
            CommonData.setTotal((int) total);
            CommonData.setPages(basePageInfo.getPage());
            CommonData.setLimit(basePageInfo.getLimit());
            CommonData.setPage(basePageInfo.getPage());
            CommonData.setList(list);
            return CommonData;

    }

    /**
     * @description : TODO 进入优惠券详情页面方法是现实
     * @return com.cskaoyan.bean.MarketCoupon
     * @author wang'ben
     * @date 2022/9/8 19:09
     */
    @Override
    public MarketCoupon selectCouponInfoById(Integer id) {
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(id);
        marketCoupon.setStartTime(new Date());
        marketCoupon.setEndTime(new Date());
        return marketCoupon;
    }


    /**
     * @description : TODO 删除优惠券方法的实现
     * @return void
     * @author wang'ben
     * @date 2022/9/8 19:09
     */
    @Override
    public void deleteCouponInfo(MarketCoupon marketCoupon) {
        marketCoupon.setDeleted(true);
        marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
    }

    /**
     * @description : TODO 修改优惠券状态方法的实现
     * @return void
     * @author wang'ben
     * @date 2022/9/8 19:37
     */
    @Override
    public void updateCouponStates(MarketCoupon marketCoupon) {
        marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
    }
}
